﻿using Domain.Material_Management;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules;
using MES.Server.API.Dto.Warehouse;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Production_Modules
{
    /// <summary>
    /// 物料库存查询命令处理
    /// </summary>
    public class InventoryQueryCommandHandler:IRequestHandler<InventoryQueryCommand,ApiResult<ApiPaging<WarehouseDTO>>>
    {
        IRepository<WarehouseModel> WareRep;
        IRepository<MaterialGroupModel> GrouplRep;
        IRepository<ProductMaterialsModel> ProductRep;
        /// <summary>
        /// 物料库存
        /// </summary>
        IRepository<MaterialInventoryModel> MaterialInventoryRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="wareRep"></param>
        /// <param name="grouplRep"></param>
        /// <param name="productRep"></param>
        /// <param name="materialInventoryRep"></param>
        public InventoryQueryCommandHandler(IRepository<WarehouseModel> wareRep, IRepository<MaterialGroupModel> grouplRep, IRepository<ProductMaterialsModel> productRep, IRepository<MaterialInventoryModel> materialInventoryRep)
        {
            WareRep = wareRep;
            GrouplRep = grouplRep;
            ProductRep = productRep;
            MaterialInventoryRep = materialInventoryRep;
        }

        public Task<ApiResult<ApiPaging<WarehouseDTO>>> Handle(InventoryQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<WarehouseDTO>> result = new ApiResult<ApiPaging<WarehouseDTO>>();
            result.Code=StatusCode.Success;
            result.Message=StatusCode.Success.ToString();
            var list = from a in WareRep.GetAll()
                       join b in MaterialInventoryRep.GetAll() on a.Id equals b.WarehouseId
                       join c in GrouplRep.GetAll() on b.MaterialGroupId equals c.Id 
                       join d in ProductRep.GetAll() on b.ProductMaterialsId equals d.Id
                       where !b.IsDel
                       select new WarehouseDTO
                       {
                           Id = b.Id,
                           WarehouseCode = a.WarehouseCode,
                           WarehouseName = a.WarehouseName,
                           WarehouseNum = a.WarehouseNum,
                           WarehouseIdSon = a.WarehouseIdSon,
                           WarehouseAddr = a.WarehouseAddr,
                           WarehouseArea = a.WarehouseArea,
                           WarehouseDesc = a.WarehouseDesc,
                           WarehouseId=b.WarehouseId,
                           CurrentInventory = b.CurrentInventory,
                           InventoryCap = b.InventoryCap,
                           InventoryFloor = b.InventoryFloor,
                           MaterialGroupId = b.MaterialGroupId,
                           ProductMaterialsId = b.ProductMaterialsId,
                           OutboundStatus=b.OutboundStatus,
                           MaterialGroupCode = c.MaterialGroupCode,
                           MaterialGroupName = c.MaterialGroupName,
                           MaterialGroupType = c.MaterialGroupType,
                           ProcessFlowId = c.ProcessFlowId,
                           MaterialGroupDesc = c.MaterialGroupDesc,
                           ProductMaterialsCode = d.ProductMaterialsCode,
                           ProductMaterialsName = d.ProductMaterialsName,
                           ProductMaterialsType = d.ProductMaterialsType,
                           ProductMaterialsSpecs = d.ProductMaterialsSpecs,
                           ProductMaterialsUnit = d.ProductMaterialsUnit,
                           ProductMaterialsforewarn = d.ProductMaterialsforewarn,
                           ProductMaterialsDesc = d.ProductMaterialsDesc

                       };
            int totalcount = list.Count();
            int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.Size);
            list = list.OrderByDescending(x => x.WarehouseId).Skip((request.Index-1) * request.Size).Take(request.Size);
            ApiPaging <WarehouseDTO> ping = new ApiPaging<WarehouseDTO>();
            ping.Totalcount=totalcount;
            ping.TotalPage = list.ToList();
            result.Data = ping;
            return Task.FromResult(result);
        }
    }
}
